<?php 

namespace app\controller\bossManager;

use app\service\AppViewService;
use app\validate\AppViewValidate;
use app\validate\PageValidate;

class AppViewController extends WebBossController{

    public function initValidate(){
        $this->validate = new AppViewValidate();
    }

    public function list_action(){
        $param = $this->request->param();
        $page = $this->check('pageInfo',null,(new PageValidate()));
        $data = AppViewService::getListInfo($param,$page);
        foreach($data as &$d){
            if(strlen($d['module']) > 1){
                $d['module'] = @json_decode($d['module'],true);
            } 
        }
        $total = AppViewService::getListCountInfo($param);
        return self::ok(self::formatPageData($page,$total,$data));
    }

    public function detail_action(){
        $input = $this->check();
        $res = AppViewService::getBase($input['id']);
        if($res){
            if(strlen($res['module']) > 1){
                $res['module'] = @json_decode($res['module'],true);
            }
            $res['clientType'] = $res['client_type'];
        }
        return self::ok($res);
    }

    public function copy_action(){
        $input = $this->check();
        $res = AppViewService::copy($input['id']);
        return self::ok($res);
    }

    public function edit_action(){
        $input = $this->check();
        $res = AppViewService::edit($input);
        return self::ok($res);
    }

    public function del_action(){
        $input = $this->check();
        $res = AppViewService::del($input['id']);
        return self::ok($res);
    }

    /**
     * 可用模块信息
     */
    public function module_info_action(){
        return self::ok(AppViewService::moduleFullInfo());
    }

    /**
     * 匹配检测
     */
    public function check_action(){
        $input = $this->check();
        $res = AppViewService::matchModule($input);
        if($res){
            if(strlen($res['module'])){
                $res['module'] = @json_decode($res['module'],true);
            }
        }
        else{
            $res = [
                'module' => [],
            ];
        }
        return self::ok($res);
    }
}
